package com.hrt.demo.wsb.sms;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.bean.SMSModual;
import com.hrt.manage.sms.modual.service.SMSModualService;
import com.hrt.thirtyparty.sms.SMSModualHandler;
import com.hrt.thirtyparty.sms.SMSModualHandlerImpl;
import com.hrt.thirtyparty.sms.SMSSender;
import com.hrt.util.RamUtil;


@Controller
@RequestMapping("demo/sms")
public class SMSTestController {

	
	private SMSSender sender;
	
	@Autowired
	private SMSModualService modualService;
	
	
	private SMSModualHandler handler;
	
	@RequestMapping("/show")
	public ModelAndView show()
	{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("manage/demo/sms/index");
		return mv;
	}
	
	
	public SMSTestController() {
		super();
		sender=new SMSSender();
	}


	@RequestMapping(value="/sendSms")
	public ModelAndView sendSMS(@RequestParam("phones")String phones,@RequestParam("bussinessCode")Integer business)
	{
		
		
		ModelAndView mv=new ModelAndView();
		mv.setViewName("manage/demo/sms/index");
		System.out.println("hello world...");
		handler=new SMSModualHandlerImpl();
		SMSModual modual=modualService.getModualByBusinessCode(business);
		if(modual!=null){
		Map<String, Object> params=new HashMap<String, Object>();
		String []phoneArr=phones.split(",");
		
		if(phoneArr.length>0){
		for(String phone:phoneArr){
			params.put("loginName", phone);
			params.put("vaildCode", RamUtil.getMobileVaildCode());
			String content=handler.genarateSMSContent(business, params, modual.getModualContent());
			
			sender.sendSMS(content, phone);
		}
		}
		}
	

		return mv;
		
	}
	
	
}
